﻿//-----------------------------------------------------------------------
// <copyright file="RepositoryProviderBase.cs" company="FastNET Services">
//     Copyright (c) 2008-2013. All Rights Reserved.
// </copyright>
// <license>
//     GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings

using System;
using System.Collections.Generic;
using System.Configuration.Provider;
using System.Globalization;
using FastNet.RepositorySystem.Interfaces;
using FastNet.RepositorySystem.Properties;

#endregion

namespace FastNet.RepositorySystem.Configuration
{
    /// <summary>
    /// Base abstract class for a configurable repository provider
    /// </summary>
    public abstract class RepositoryProviderBase : ProviderBase, IDataProvider
    {
        #region Variables

        private static readonly Dictionary<Type, IRepository> __repositories = new Dictionary<Type, IRepository>();

        #endregion

        #region Properties

        /// <summary>
        /// Gets a reference to the collection of repositories
        /// </summary>
        protected static Dictionary<Type, IRepository> Repositories { get { return __repositories; } }

        #endregion

        #region Methods

        /// <summary>
        /// Retrieves a repository for data related actions
        /// </summary>
        /// <typeparam name="T">A <typeparamref name="T"/> based reference to the type of repository needed</typeparam>
        /// <returns>A <typeparamref name="T"/> repository available to use</returns>
        public T GetRepository<T>() where T : IRepository
        {
            if (__repositories.ContainsKey(typeof(T)))
            {
                return (T)__repositories[typeof(T)];
            }

            throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.GetRepositoryInvalid, typeof(T)));
        }

        #endregion
    }
}